﻿<?php
require_once 'Zend/Db.php';
require_once 'Zend/Config/Ini.php';
require_once 'FtpHelper.php';
class commands{
    private function db()
    {   
        $params = array ('host' => '127.0.0.1',
                 'port' => '3306',
                 'username' => 'root',
                 'password' => 'soooner.com',
                 'charset' => 'UTF8',
                 'dbname'   => 'minicms');
        $db = Zend_Db::factory('PDO_MYSQL', $params);
        return $db;
    }
    
    function getConfig()//获取配置信息
    {
    	$db = $this->db();
    	$configrows = $db->fetchAll("select configname, configvalue from config");//获取配置信息
        $configs = array();
    	if (!empty($configrows)){
	       foreach ($configrows as $item){
	          $configs[$item['configname']] = $item['configvalue'];}
    	}//配置信息获取完成
    	return $configs;
    }
    
    function getEpgMap()//获取epg名称和对应关系,暂时不用
    {
    	$db = $this->db();
    	$maprows = $db->fetchAll("select epgurl, contentid from c_zhibo where epgtype =1 and epgurl <>''");//获取信息
        $maps = array();
    	if (!empty($maprows)){
	       foreach ($maprows as $item){
	          $maps[$item['epgurl']] = $item['contentid'];}
    	}//配置信息获取完成
    	return $maps;
    }
    
    function clearEpgCommand($day=7)//删除epg中的过期数据,默认删除7天前的数据
    {
    	$db = $this->db();
		$fromtime = time() - ($day * 24 * 60 * 60); 
		$fromtime = date('Y-m-d', $fromtime);
		$db->query("delete from epg where start < '".$fromtime."'");
    }
    
    function clearContentCommand()// 把版权到期节目放入回收站
    {
    	$db = $this->db();
    	$result = $db->fetchCol("SELECT tablename FROM model where modeltype=1");
    	foreach($result as $key=>$v)
    	{
    		$db->query("UPDATE content c LEFT JOIN $v extb  ON extb.contentid = c.contentid"
				." SET c.status=0,c.updatetime=UNIX_TIMESTAMP(NOW()) where extb.endtime<now() and c.contenttype=1;");
    	}
    }
    
    function clearRecycleCommand()//定期清空回收站
    {
    	echo "start";
    	$db = $this->db();
    	$configs = $this->getConfig();
    	$prehttp = $configs["prehttp"];
		$ftphost = $configs["ftphost"];
		$ftpuser = $configs["ftpuser"];
		$ftppasswd = $configs["ftppasswd"];
		$ftpport = $configs["ftpport"];
		//////////////////////////////////////////////////////////////
		$ftp = new FtpHelper($ftphost,$ftpport,$ftpuser,$ftppasswd); // 打开FTP连接
		$result = $db->fetchAll("SELECT tablename FROM model where modeltype=1");
		foreach($result as $row){
			$sqlselect = "select c.contentid,extb.urllow,extb.urlmedium,extb.urlhigh from content c"
						." left join ".$row['tablename']." extb on c.contentid=extb.contentid"
						." where c.status=0 and DATE_ADD(from_unixtime(c.updatetime),INTERVAL 2 MONTH) <NOW();";
			$fileList = $db->fetchAll($sqlselect);
			foreach($fileList as $file)
			{
				try{
					$path = $file['urllow'];//低
					if(strpos($path,$prehttp)>-1){
						$path = str_ireplace($prehttp,"",$path);
						$ftp->del_file($path);}
					$path = $file['urlmedium'];//中
					if(strpos($path,$prehttp)>-1){
						$path = str_ireplace($prehttp,"",$path);
						$ftp->del_file($path);}
					$path = $file['urlhigh'];//高
					if(strpos($path,$prehttp)>-1){
						$path = str_ireplace($prehttp,"",$path);
						$ftp->del_file($path);}
					$sql = "delete c,extb from content c left join ".$row['tablename']." extb on c.contentid=extb.contentid"
				 		." where c.contentid=".$file['contentid'].";";
				 	echo $sql;
					$db->query($sql);
					print_r($file);
				}catch(Exception $e){
					print_r($e);
					exit;
				}	
			}
		}
		$ftp->close(); // 关闭FTP连接
		echo "end";
    }
 	
    function searchCommand()//重建索引命令
    {
    	//echo "begin";
    	$db = $this->db();
   		try{
	    	$db->beginTransaction();
	    	$result = $db->query("truncate table documents");
	    	$result = $db->query("insert into minicms.documents (group_id, contentid, epgid, date_added, title, content)select 0,vid,0,now(),title,concat(title,tag1,tag2,tag3,content) from live where ipad=0");//非ipad节目
	    	$result = $db->query("insert into minicms.documents (group_id, contentid, epgid, date_added, title, content)select 2,vid,0,now(),title,concat(title,tag1,tag2,tag3,content) from live where ipad=1");//ipad节目
	    	$result = $db->query("insert into minicms.documents (group_id, contentid, epgid, date_added, title, content)select 1,vid,0,now(),title,concat(title,director,act,tag1,tag2,tag3) from vod");
	    	$db->commit();
	    }
		catch (Exception $e){
			$db->rollBack();
			syslog(LOG_INFO, ": search - ".$e);
		}
		//echo "end";
    }
    
	function createEpgCommand()
	{
		$dir ="/data/epg/";
		$map = $this->getEpgMap();
		if ($dh = opendir($dir)) {
	        while (($file = readdir($dh)) !== false) {
	        	if(strpos($file,".xml"))
	        	{
	        		try{
	        			$this->createEpg($file,$map);
	        		}
	        		catch (Exception $e){
	        			echo 'Caught exception: ',  $e->getMessage(), "\n";
	        		}	        		
	        	}
	        }
	        closedir($dh);
	    }
	}
	
	function createEpg($xmlfile,$map)//从文件导入epg信息
    {
		//$filename = "/tmp/c.xml";
		echo $xmlfile;
		$dir ="/data/epg/";//"江苏卫视.xml"=>"1680","江苏城市.xml"=>"1682","江苏影视.xml"=>"1683","江苏教育.xml"=>"206","江苏体育.xml"=>"5441","江苏综艺.xml"=>"1681",
		/*$map=array("江苏卫视.xml"=>"1680","江苏城市.xml"=>"1682","江苏影视.xml"=>"1683",
		"江苏教育.xml"=>"206","江苏体育.xml"=>"5441","江苏综艺.xml"=>"1681",
		"央视高清.xml"=>"186","中央新闻.xml"=>"185",
		"中央少儿.xml"=>"184","中央音乐.xml"=>"183",
		"CCTV-12.xml"=>"182","CCTV-11.xml"=>"179",
		"CCTV-10.xml"=>"178","CCTV-9.xml"=>"177","CCTV-8.xml"=>"176",
		"CCTV-7.xml"=>"175","CCTV-6.xml"=>"174","CCTV-5.xml"=>"173","CCTV-4.xml"=>"172",
		"CCTV-3.xml"=>"170","CCTV-2.xml"=>"169","CCTV-1.xml"=>"168","重庆卫视.xml"=>"188",
		"吉林卫视.xml"=>"189","辽宁卫视.xml"=>"190","东南卫视.xml"=>"191","江西卫视.xml"=>"192",
		"四川卫视.xml"=>"194","河北卫视.xml"=>"196","河南卫视.xml"=>"197","卡酷卡通.xml"=>"198",
		"深圳卫视.xml"=>"199","山西卫视.xml"=>"200","兵团卫视.xml"=>"201","内蒙古卫视.xml"=>"202",
		"甘肃卫视.xml"=>"203","宁夏卫视.xml"=>"204","青海卫视.xml"=>"205","山东卫视.xml"=>"207",
		"安徽卫视.xml"=>"208","贵州卫视.xml"=>"209","金鹰卡通.xml"=>"210","天津卫视.xml"=>"187",
		"旅游卫视.xml"=>"193","广西卫视.xml"=>"195","湖南卫视.xml"=>"1769","东方卫视.xml"=>"1768",
		"北京卫视.xml"=>"1761");*/
		if($xmlfile){
			try{
				$ok = false;
				$contentid = $map[$xmlfile];
				if(!$contentid)
				{
					unlink($dir.$xmlfile);
					return;
				}
				$db = $this->db();
				$fp = fopen($dir.$xmlfile, 'r');
				$xmldata = fread($fp, filesize($dir.$xmlfile));
				fclose($fp);
				$doc = new DOMDocument();
				$doc->loadXML($xmldata);
				$items = $doc->getElementsByTagName("Event");
				$eventidbase = 0;//偏移值
				$timebase = "";
				$realeventid = 0;//初始值为0
				foreach($items as $item)
				{
					$begintime = $item->getAttribute("begintime");
					$duration = $item->getAttribute("duration");
		            $eventid = $realeventid;//$item->getAttribute("eventid");
		            $Y = substr($begintime,0,4);
		            $m = substr($begintime,4,2);
		            $d = substr($begintime,6,2);
		            $H = substr($begintime,8,2);
		            $i = substr($begintime,10,2);
		            $s = substr($begintime,12,2);
		            $addH = substr($duration,0,2);
		            $addi = substr($duration,2,2);
		            $adds = substr($duration,4,2);		            
		            if(($timebase !="") && ($timebase!="$Y$m$d"))//计算偏移值
		            {
		            	$eventidbase = $eventid;
		            	$db->query("update epg set flag = 1 where epgkey like '$contentid-$timebase%'");
		            	//$db->query("delete from epg where epgkey like '$contentid-$timebase%' and epgkey > '$contentid-$timebase-$eventidbase'");
		            }
		            $eventid -= $eventidbase;
		            $timebase = "$Y$m$d";		            
		            $epgkey = "$contentid-$Y$m$d-$eventid";
		            $start = date("Y-m-d H:i:s",mktime($H,$i,$s,$m,$d,$Y));
		            $end = date("Y-m-d H:i:s",mktime(0+$H+$addH,0+$i+$addi,0+$s+$adds,$m,$d,$Y));
		            $name = $item->getElementsByTagName("EventText")->item(0)->getElementsByTagName("Name")->item(0)->nodeValue;
		            $name = trim($name);
		            $num = $db->fetchOne("select count(*) from epg where epgkey = '$epgkey'");
		            if($num =="0"){	$db->query("insert into epg (contentid, title, start, end, epgkey,flag) values('$contentid' ,'$name','$start','$end','$epgkey','0')");}
		            else{$db->query("update epg set contentid='$contentid' ,title='$name',start='$start',end='$end',flag = '0' where epgkey = '$epgkey'");}
		            $realeventid +=1;		            
		            $ok = true;
		        }
		        if($ok)
		        {
		        	//$db->query("delete from epg where flag = 1");
		        	unlink($dir.$xmlfile);
		        }
			}
			catch (Exception $e){
				echo 'Caught exception: ',  $e->getMessage(), "\n";
				syslog(LOG_INFO, ": commands - clearRecycleCommand." .$e->getMessage());
			}
    	}
    }
    
	
    function createXmlNewCommand($level)
	{
		//$level="1|1|1,2,3,4,5,67"参数说明：首页和栏目页，节目单，详细页	
		$dir ="/data/interfaces/";
		$bizsite = "http://www.bigtv.com.cn";
		$db = $this->db();
		$levels =  explode('|', $level);
		if($levels[0])//生成首页和栏目页等
		{
			system("wget '$bizsite/interface/GetNavigation.php' --save-cookies=cookies --keep-session-cookies -O ".$dir."GetNavigation.xml");
			system("wget '$bizsite/interface/GetChannel.php' --load-cookies=cookies --keep-session-cookies -O ".$dir."GetChannel.xml");
			$catlist = $db->fetchAll("select catid from category where parentid =0");
			array_push($catlist,array("catid"=>"0"));
			foreach($catlist as $cat)
			{
				if(!$cat['catid'])$cat['catid']="0";
				system("wget '$bizsite/interface/GetContentTop.php?id=".$cat['catid']."'  --load-cookies=cookies --keep-session-cookies -O ".$dir."GetContentTop/".$cat['catid'].".xml");
				$this->pageXmlContentTop($dir,"GetContentTop/".$cat['catid'].".xml",$cat['catid'],$bizsite);
			}
			$poslist = $db->fetchAll("select posid from position");
			foreach($poslist as $pos)
			{
				system("wget '$bizsite/interface/GetContentGroup.php?id=".$pos['posid']."&topic=0'  --load-cookies=cookies --keep-session-cookies -O ".$dir."GetContentGroup/".$pos['posid']."_0.xml");
				$this->pageXmlContentGroup($dir,"GetContentGroup/".$pos['posid']."_0.xml",$pos['posid'],0,$bizsite);
			}
		}
		if($levels[1])//生成节目单
		{
			$chanellist = $db->fetchAll("select contentid from content where isonline =1 and STATUS = 99 and contenttype =0");
			foreach($chanellist as $chanel)
			{
				for($i=-2;$i<3;$i++){
					$dt = date('Y-m-d',time() - ($i * 24 * 60 * 60));
					system("wget '$bizsite/interface/GetListEpg.php?id=".$chanel['contentid']."&date=$dt'  --load-cookies=cookies --keep-session-cookies -O ".$dir."GetListEpg/".$chanel['contentid']."_".$dt.".xml");
				}
			}
		}
		if($levels[2])//详细页
		{
			$where = "  catid in (80,".$levels[2].") ";//默认生成80栏目
			if($levels[2]=="all")//所有
				$where = " 1=1 ";
				
			$catlist = $db->fetchAll("select catid,parentid from category where parentid >0 and $where");
			foreach($catlist as $cat)
			{
				system("wget '$bizsite/interface/GetContentGroup.php?id=".$cat['catid']."&topic=".$cat['parentid']."'  --load-cookies=cookies --keep-session-cookies -O ".$dir."GetContentGroup/".$cat['catid']."_".$cat['parentid'].".xml");
				$this->pageXmlContentGroup($dir,"GetContentGroup/".$cat['catid']."_".$cat['parentid'].".xml",$cat['catid'],$cat['parentid'],$bizsite);
			}
			$hotsubjectlist = $db->fetchAll("select catid from category where $where");
			foreach($hotsubjectlist as $hotsubject)
			{
				system("wget '$bizsite/interface/GetHotSubject.php?id=".$hotsubject['catid']."'  --load-cookies=cookies --keep-session-cookies -O ".$dir."GetHotSubject/".$hotsubject['catid'].".xml");
			}
			//GetListSubject
			$chanellist = $db->fetchAll("select contentid from content where isonline =1 and STATUS = 99 and contenttype =2 and $where");
			foreach($chanellist as $chanel)
			{
				system("wget '$bizsite/interface/GetListSubject.php?id=".$chanel['contentid']."'  --load-cookies=cookies --keep-session-cookies -O ".$dir."GetListSubject/".$chanel['contentid'].".xml");
				$this->pageXmlListSubject($dir,"GetListSubject/".$chanel['contentid'].".xml",$chanel['contentid'],$bizsite);
			}
			$contentlist = $db->fetchAll("select contentid from content where isonline =1 and contenttype <3 and inputtime=updatetime and $where");
			foreach($contentlist as $chanel)
			{
				//$db->query("update content set updatetime = '".time()."' where contentid = ".$chanel['contentid']);
				system("wget '$bizsite/interface/GetAdv.php?id=".$chanel['contentid']."'  --load-cookies=cookies --keep-session-cookies -O ".$dir."GetAdv/".$chanel['contentid'].".xml");
				system("wget '$bizsite/interface/GetContentTips.php?id=".$chanel['contentid']."'  --load-cookies=cookies --keep-session-cookies -O ".$dir."GetContentTips/".$chanel['contentid'].".xml");
			}
		}
		system("find $dir -type f  |xargs grep error |cut -d ':' -f 1 |xargs rm");//删除错误文件
		system("rsync -azr --progress -e \"ssh -p3009\" /data/interfaces/ 111.208.58.36:/data/webroot/static/public/iface/");
		system("rsync -azr --progress -e \"ssh -p3009\" /data/interfaces/ 111.208.58.37:/data/webroot/static/public/iface/");
	}
    function createXmlCommand($level)//静态化接口数据
	{
		$dir ="/data/interfaces/";
		$bizsite = "http://www.bigtv.com.cn";
		$db = $this->db();
		if($level=="1")//创建session文件比较多，只有level=1才执行
		{
			system("wget '$bizsite/interface/GetNavigation.php' --save-cookies=cookies --keep-session-cookies -O ".$dir."GetNavigation.xml");
			system("wget '$bizsite/interface/GetChannel.php' --load-cookies=cookies --keep-session-cookies -O ".$dir."GetChannel.xml");
			$catlist = $db->fetchAll("select catid from category where parentid =0");
			array_push($catlist,array("catid"=>"0"));
			foreach($catlist as $cat)
			{
				if(!$cat['catid'])$cat['catid']="0";
				system("wget '$bizsite/interface/GetContentTop.php?id=".$cat['catid']."'  --load-cookies=cookies --keep-session-cookies -O ".$dir."GetContentTop/".$cat['catid'].".xml");
				$this->pageXmlContentTop($dir,"GetContentTop/".$cat['catid'].".xml",$cat['catid'],$bizsite);
			}
			$poslist = $db->fetchAll("select posid from position");
			foreach($poslist as $pos)
			{
				system("wget '$bizsite/interface/GetContentGroup.php?id=".$pos['posid']."&topic=0'  --load-cookies=cookies --keep-session-cookies -O ".$dir."GetContentGroup/".$pos['posid']."_0.xml");
				$this->pageXmlContentGroup($dir,"GetContentGroup/".$pos['posid']."_0.xml",$pos['posid'],0,$bizsite);
			}
			$catlist = $db->fetchAll("select catid,parentid from category where parentid >0");
			foreach($catlist as $cat)
			{
				system("wget '$bizsite/interface/GetContentGroup.php?id=".$cat['catid']."&topic=".$cat['parentid']."'  --load-cookies=cookies --keep-session-cookies -O ".$dir."GetContentGroup/".$cat['catid']."_".$cat['parentid'].".xml");
				$this->pageXmlContentGroup($dir,"GetContentGroup/".$cat['catid']."_".$cat['parentid'].".xml",$cat['catid'],$cat['parentid'],$bizsite);
			}
			$hotsubjectlist = $db->fetchAll("select catid from category");
			foreach($hotsubjectlist as $hotsubject)
			{
				system("wget '$bizsite/interface/GetHotSubject.php?id=".$hotsubject['catid']."'  --load-cookies=cookies --keep-session-cookies -O ".$dir."GetHotSubject/".$hotsubject['catid'].".xml");
			}
			//GetListSubject
			$chanellist = $db->fetchAll("select contentid from content where isonline =1 and STATUS = 99 and contenttype =2");
			foreach($chanellist as $chanel)
			{
				system("wget '$bizsite/interface/GetListSubject.php?id=".$chanel['contentid']."'  --load-cookies=cookies --keep-session-cookies -O ".$dir."GetListSubject/".$chanel['contentid'].".xml");
				$this->pageXmlListSubject($dir,"GetListSubject/".$chanel['contentid'].".xml",$chanel['contentid'],$bizsite);
			}
			$chanellist = $db->fetchAll("select contentid from content where isonline =1 and STATUS = 99 and contenttype =0");
			system("find $dir.'GetListEpg/' -type f -name *.xml  |xargs rm");//删除旧的epg文件
			foreach($chanellist as $chanel)
			{
				for($i=-2;$i<3;$i++){
					$dt = date('Y-m-d',time() - ($i * 24 * 60 * 60));
					system("wget '$bizsite/interface/GetListEpg.php?id=".$chanel['contentid']."&date=$dt'  --load-cookies=cookies --keep-session-cookies -O ".$dir."GetListEpg/".$chanel['contentid']."_".$dt.".xml");
				}
			}
			//拷贝文件
			system("find $dir -type f  |xargs grep error |cut -d ':' -f 1 |xargs rm");//删除错误文件
			system("scp -r -P 3009 /data/interfaces/* 111.208.58.37:/data/webroot/static/public/iface ");
			system("scp -r -P 3009 /data/interfaces/* 111.208.58.36:/data/webroot/static/public/iface ");
		}
		else if($level=="2")
		{
			//$fromtime = time() - (2 * 24 * 60 * 60); 
			$contentlist = $db->fetchAll("select contentid from content where isonline =1 and contenttype <3 and updatetime = inputtime order by contentid desc  limit 100");
			foreach($contentlist as $chanel)
			{
				$db->query("update content set updatetime = '".time()."' where contentid = ".$chanel['contentid']);
				system("wget '$bizsite/interface/GetAdv.php?id=".$chanel['contentid']."'  --load-cookies=cookies --keep-session-cookies -O ".$dir."GetAdv/".$chanel['contentid'].".xml");
				system("wget '$bizsite/interface/GetContentTips.php?id=".$chanel['contentid']."'  --load-cookies=cookies --keep-session-cookies -O ".$dir."GetContentTips/".$chanel['contentid'].".xml");
			}
			//system("find $dir -type f  |xargs grep error |cut -d ':' -f 1 |xargs rm");//删除错误文件
			system("scp -r -P 3009 /data/interfaces/GetAdv/* 111.208.58.37:/data/webroot/static/public/iface/GetAdv ");
			system("scp -r -P 3009 /data/interfaces/GetAdv/* 111.208.58.36:/data/webroot/static/public/iface/GetAdv ");
			system("scp -r -P 3009 /data/interfaces/GetContentTips/* 111.208.58.37:/data/webroot/static/public/iface/GetContentTips ");
			system("scp -r -P 3009 /data/interfaces/GetContentTips/* 111.208.58.36:/data/webroot/static/public/iface/GetContentTips ");
			system("find ".$dir."/GetAdv/ -type f  |cut -d ':' -f 1 |xargs rm");//删除详细信息文件
			system("find ".$dir."/GetContentTips/ -type f  |cut -d ':' -f 1 |xargs rm");//删除详细信息文件
		}
		
	}
	function pageXmlContentTop($dir,$xmlfile,$catid,$bizsite)
	{
		$fp = fopen($dir.$xmlfile, 'r');
		$xmldata = fread($fp, filesize($dir.$xmlfile));
		fclose($fp);
		$doc = new DOMDocument();
		$doc->loadXML($xmldata);
		$totle = $doc->getElementsByTagName("totle")->item(0)->nodeValue;
		for($i=0;$i<$totle;$i++)
		{
			system("wget '$bizsite/interface/GetContentTop.php?id=".$catid."&page=$i' --load-cookies=cookies --keep-session-cookies -O ".$dir."GetContentTop/".$catid."_".$i.".xml");
		}
	}
	function pageXmlContentGroup($dir,$xmlfile,$catid,$topicid,$bizsite)
	{
		$fp = fopen($dir.$xmlfile, 'r');
		$xmldata = fread($fp, filesize($dir.$xmlfile));
		fclose($fp);
		$doc = new DOMDocument();
		$doc->loadXML($xmldata);
		$totle = $doc->getElementsByTagName("totle")->item(0)->nodeValue;
		for($i=0;$i<$totle;$i++)
		{
			system("wget '$bizsite/interface/GetContentGroup.php?id=".$catid."&topic=".$topicid."&page=$i' -O ".$dir."GetContentGroup/".$catid."_".$topicid."_".$i.".xml");
		}
	}
	function pageXmlListSubject($dir,$xmlfile,$id,$bizsite)
	{
		$fp = fopen($dir.$xmlfile, 'r');
		$xmldata = fread($fp, filesize($dir.$xmlfile));
		fclose($fp);
		$doc = new DOMDocument();
		$doc->loadXML($xmldata);
		$totle = $doc->getElementsByTagName("totle")->item(0)->nodeValue;
		for($i=0;$i<$totle;$i++)
		{
			system("wget '$bizsite/interface/GetListSubject.php?id=".$id."&page=$i'  --load-cookies=cookies --keep-session-cookies -O ".$dir."GetListSubject/".$id."_".$i.".xml");
		}
	}
	/**
	 * 5分钟采集一次
	 * 1,6,11,16,21,26,31,36,41,46,51,56 * * * * php /data/webroot/minicms/minicms/library/Common/Command/commands.php getDeviceInfo
	 * @return unknown_type
	 */
	function getDeviceInfoCommand()
	{
		$ip = "127.0.0.1";
		$oid = "1.3.6.1.4.1.3000.6";
		$time = date("Y-m-d_H:i",time()-60);
		$path = "/soooner/qos_record/";//
		$livefilename = $path."soooner-live-sp-online-info-".$time."-uuidscs76.txt";
		$vodfilename = $path."soooner-vod-sp-online-info-".$time."-uuidscs76.txt";
//		echo $livefilename;
//		echo "\n";
//		echo $vodfilename;
		$cpu = 0;
		$mem = 0;
		$net = 0;
		$iowait = 0;
		$diskfree = 0;
		$liveOnline = 0;
		$vodOnline = 0;
		//---device----
		try{
			//取设备信息
			$snmpResults = snmpwalkoid($ip, "soooner", $oid);
			$arr = array();
			$index = 0;
			for (reset($snmpResults); $i = key($snmpResults); next($snmpResults))
			{
			    $arr[$index] = str_replace("INTEGER: ","",$snmpResults[$i]);
			    $index++;
			}
			$cpu = $arr[0];
			$mem = $arr[1];
			//$net = (int)((int)$arr[2]/1024);
			$net = (int)$arr[2];
			$iowait = $arr[3];
			$diskfree = $arr[4];
		}catch (Exception $e)
		{}
		//---vod---
		try {			
			 if(file_exists($vodfilename)){
			 	$fp = fopen($vodfilename, 'r');
				$record = fread($fp, filesize($vodfilename));
				fclose($fp);
				$arr_vod = explode('	',$record);
				//print_r($arr_vod);
				$vodOnline = $arr_vod[3];
			 }
		}catch(Exception $e1)
		{}
		//---live---
		try{
			if(file_exists($vodfilename)){
			 	$fp = fopen($vodfilename, 'r');
				$record = fread($fp, filesize($vodfilename));
				fclose($fp);
				$arr_live = explode('	',$record);
				$liveOnline = $arr_live[3];
			}
		}catch(Exception $e2)
		{}
		$command = "rrdtool update /data/web/rrd/report/report.rrd ".time().":".$cpu.":".$diskfree.":".$mem.":".$net.":".$vodOnline.":".$liveOnline; //  cpu, disk,mem,net,vod,live
		passthru($command);
//		$db = $this->db();
//		$sql = "insert into device_collectinfo (dci_cpu,dci_mem,dci_diskfree,dci_iowait,dci_net,dci_time,dci_live_online,dci_vod_online) values (".$cpu.",".$mem.",".$diskfree.",".$iowait.",".$net.",now(),".$liveOnline.",".$vodOnline.")";
//		$db->query($sql);
	}
}
$cmd=$argv[1]; 
//$arg=$argv[2]; 
if($cmd=="clearEpg"){//清节目单
	$test = new commands();
	$test->clearEpgCommand();
	syslog(LOG_INFO, ": commands - clearEpgCommand.");
}
elseif($cmd=="clearContent"){//把版权到期节目放入回收站
	$test = new commands();
	$test->clearContentCommand();
	syslog(LOG_INFO, ": commands - clearContentCommand.");
}
elseif($cmd=="clearRecycle"){//定期清空回收站
	$test = new commands();
	$test->clearRecycleCommand();
	syslog(LOG_INFO, ": commands - clearRecycleCommand.");
}
elseif($cmd=="search"){//重建索引
	$test = new commands();
	$test->searchCommand();
	syslog(LOG_INFO, ": commands - searchCommand.");
}
else if($cmd=="createEpg"){//清节目单
	$test = new commands();
	$test->createEpgCommand();
	syslog(LOG_INFO, ": commands - createEpgCommand.");
}
else if($cmd=="createXmlNew"){//创建所有XML文件,包括详细信息
	$test = new commands();
	$test->createXmlNewCommand($arg);
	syslog(LOG_INFO, ": commands - createXmlNew.");
}
else if($cmd=="test"){
	$test = new commands();
	$test->pageXml();
}
else if($cmd=="getDeviceInfo"){
	$test = new commands();
	$test->getDeviceInfoCommand();
}